package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量上传数据
    @Override
    public void uploadOrderSettings(List<OrderSetting> orderSettingList) {

        if (orderSettingList != null && orderSettingList.size()>0){

            for (OrderSetting orderSetting : orderSettingList) {
                //查询该数据是否存在
                Long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0){//数据存在
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {//数据不存在
                    orderSettingDao.add(orderSetting);
                }
            }
        }

    }

    //根据月份查询本月的预约信息
    @Override
    public List<Map> getOrderSettingByMonth(String date) {

        List<OrderSetting> orderSettings = orderSettingDao.getOrderSettingByMonth(date);
        List<Map> list = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            Map<String,Object> map = new HashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            list.add(map);
        }
        return list;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {

        //查询该数据是否存在
        Long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0){//数据存在
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {//数据不存在
            orderSettingDao.add(orderSetting);
        }

    }
}
